package com.lyrcsoft;

import com.lyrcsoft.advice.MyMethodBeforeAdvice;
import com.lyrcsoft.services.IUserExtendSerivce;
import com.lyrcsoft.services.UserService;
import org.aopalliance.aop.Advice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.StaticMethodMatcherPointcut;

import java.lang.reflect.Method;

/**
 * @author fujc2dev@126.com
 * @description TODO
 * @date 2024-01-17 10:52
 */
public class ProxyFactoryTest {
	public static void main(String[] args) {
		UserService target = new UserService();
		ProxyFactory factory = new ProxyFactory();
		factory.setTarget(target);
		// 这种写法都被代理了，在真实情况下，肯定是有那些方法不需要代理的。怎么办呢？
		//factory.addAdvice(new MyMethodBeforeAdvice());
		//factory.setInterfaces(IUserExtendSerivce.class);
		//factory.setExposeProxy(false);
		factory.addAdvisor(new PointcutAdvisor() {
			/**
			 *  匹配逻辑，判断代理的方法
			 * @return
			 */
			@Override
			public Pointcut getPointcut() {
				return new StaticMethodMatcherPointcut() {
					@Override
					public boolean matches(Method method, Class<?> targetClass) {
						return method.getName().equals("test");
					}
				};
			}

			/**
			 * 一段代理逻辑
			 * @return
			 */
			@Override
			public Advice getAdvice() {
				return new MyMethodBeforeAdvice();
			}

			@Override
			public boolean isPerInstance() {
				return false;
			}
		});
		UserService userService = (UserService) factory.getProxy();
		userService.test();
		//userService.a();
	}
}
